package com.mock.ems.controller.distribution;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.mock.base.util.CollectionUtil;
import com.mock.base.util.NumberUtil;
import com.mock.base.util.StringUtil;
import com.mock.ems.common.JsonResult;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by MOCK on 2017/8/13.
 */
public class DistributionConfigController extends Controller {

    public void index() {

        List<Record> recordList = Db.find("select * from distribution_ratio_config");
        if (CollectionUtil.isNotEmpty(recordList)) {
            recordList.forEach(item -> {

                item.set("ratio",(int)(item.getDouble("ratio").doubleValue()*100) + "");
                item.set("fr_consumer_ratio",(int)(item.getDouble("fr_consumer_ratio").doubleValue()*100) + "%");
                item.set("fr_recommender_ratio",(int)(item.getDouble("fr_recommender_ratio").doubleValue()*100) + "%");
                item.set("fr_service_center_ratio",(int)(item.getDouble("fr_service_center_ratio").doubleValue()*100) + "%");
                item.set("fr_cooperation_partner_ratio",(int)(item.getDouble("fr_cooperation_partner_ratio").doubleValue()*100) + "%");


                item.set("fr_consumer_factor",(int)(item.getDouble("fr_consumer_factor").doubleValue()*100) + "%");
                item.set("fr_recommender_factor",(int)(item.getDouble("fr_recommender_factor").doubleValue()*100) + "%");
                item.set("fr_service_center_factor",(int)(item.getDouble("fr_service_center_factor").doubleValue()*100) + "%");
                item.set("fr_cooperation_partner_factor",(int)(item.getDouble("fr_cooperation_partner_factor").doubleValue()*100) + "%");


                item.set("sg_merchant_ratio",(int)(item.getDouble("sg_merchant_ratio").doubleValue()*100) + "%");
                item.set("sg_consumer_ratio",(int)(item.getDouble("sg_consumer_ratio").doubleValue()*100) + "%");

                item.set("sg_merchant_factor",(int)(item.getDouble("sg_merchant_factor").doubleValue()*100) + "%");
                item.set("sg_consumer_factor",(int)(item.getDouble("sg_consumer_factor").doubleValue()*100) + "%");

            });

            setAttr("recordList",recordList);
        }
        renderJsp("/page/distribution/distribution_config.jsp");
    }

    @Before(Tx.class)
    public void update() {

        String updateKey = getPara("update_keys");
        String updateValue = getPara("update_values");

        if (StringUtil.isEmpty(updateKey) || StringUtil.isEmpty(updateValue)) {
            renderJson(new JsonResult().status("-1").show("更新失败"));
            return;
        }

        String [] updateKeys = updateKey.split(",");
        String [] updateValues = updateValue.split(",");

        String sql = "update distribution_ratio_config set ";
        for (String ukey : updateKeys) {
            sql += ukey + "= ? ,";
        }
        sql = sql.substring(0,sql.length() - 1);

        List<Double> params = new ArrayList<>();
        for (String uvalue : updateValues) {
            if (uvalue.indexOf("%") > 0) {
                uvalue = uvalue.substring(0,updateValue.length() - 1);
            }
            Double dupdateValue = NumberUtil.toDouble(uvalue);
            if (null == dupdateValue) {
                renderJson(new JsonResult().status("-3").show("数据格式不正确,请填写百分比"));
                return;
            }
            dupdateValue = dupdateValue / 100;
            params.add(dupdateValue);
        }

        int updateRow = Db.update(sql,params.toArray());
        if (updateRow == 0) {
            renderJson(new JsonResult().status("-2").show("更新失败"));
            return;
        }
        renderJson(new JsonResult().status("000").show("更新成功"));
    }
}
